package com.hiwan.unions.activity.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.main.LuanchActivity;
import com.hiwan.unions.application.AppManager;
import com.hiwan.unions.application.ConstantsValues;
import com.hiwan.unions.util.CommonUtils;
import com.hiwan.unions.util.interfaces.NavigationLiterface;
import com.hiwan.unions.view.NavigationView;
import com.hiwan.unions.view.widget.statusbar.StatusBarUtil;
import com.umeng.analytics.MobclickAgent;
import com.yanzhenjie.nohttp.Logger;

import cn.jpush.android.api.JPushInterface;
import me.yokeyword.fragmentation.SupportActivity;


/**
 * Created by haorui on 16/8/11.
 * 无MVP的activity基类
 */

public abstract class RootActivity extends SupportActivity implements NavigationLiterface {

    protected Activity mContext;
    private View mRootView;
    private View mContentView;
    protected NavigationView navigationView;
    public Button confirmButton;
    protected SharedPreferences userInfo;
    protected SharedPreferences userInfoC;
    public Bundle bundle;
    protected boolean title = true;//状态栏设置

    protected <T extends View> T getView(int id) {
        return (T) findViewById(id);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        AppManager.getInstance(this).addActivity(this);
        this.userInfo = getSharedPreferences(ConstantsValues.INIT_USER_INFO,
                Context.MODE_PRIVATE);
        this.userInfoC = getSharedPreferences(ConstantsValues.APP,
                Context.MODE_PRIVATE);
        mContext = this;
        //网络数据加载
        if (savedInstanceState != null) {
            AppManager.getInstance(this).finishAllActivity();
            Intent intent = new Intent(this, LuanchActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            android.os.Process.killProcess(android.os.Process.myPid());  //结束进程之前可以把你程序的注销或者退出代码放在这段代码之前
        }
        onViewCreated();
        initData();
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        mRootView = LayoutInflater.from(this).inflate(R.layout.activity_root, null, false);
        mContentView = LayoutInflater.from(this).inflate(layoutResID, null, false);
        // content
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mContentView.setLayoutParams(params);
        RelativeLayout mContainer = (RelativeLayout) mRootView.findViewById(R.id.container);
        mContainer.addView(mContentView);
        setContentView(mRootView);
        navigationView = getView(R.id.navigationView);
        navigationView.SetNavigationLiterface(this);
        if (title) {
            setStatusBarColor(R.color.apptheme, true);
        } else {
            Logger.i("执行？？？");
            // 当SDK版本号为19以上版本时
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
            setNavigationView2();
        }
        if (findView(R.id.confirmButton) != null) {
            confirmButton = findView(R.id.confirmButton);
            confirmButton.setOnClickListener(listener);
        }
    }

    public <T extends View> T findView(int viewId) {
        return (T) mContentView.findViewById(viewId);
    }

    public <T extends View> T findView(View view, int viewId) {
        return (T) view.findViewById(viewId);
    }

    /**
     * 设置状态栏属性
     *
     * @param colorResId 状态栏颜色
     * @param isLight    状态栏字体  true:浅色 false:深色
     */
    public void setStatusBarColor(int colorResId, boolean isLight) {
//        if (isLight) {
//            //设置状态栏字体为浅色
//            StatusBarUtil.StatusBarDarkMode(this);
//        } else {
//            //设置状态栏字体为深色
//            StatusBarUtil.StatusBarLightMode(this);
//        }
        // 设置透明状态栏
        StatusBarUtil.setColor(this, CommonUtils.getColor(colorResId), 0);
    }

    public void setNavigationView2() {
        navigationView.setVisibility(View.GONE);
    }

    private View.OnClickListener listener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.confirmButton:
                    //跳转
                    IntentConfirm();
                    break;
            }
        }
    };

    /**
     * confirmButton执行跳转页面
     */
    protected void IntentConfirm() {
        hintKbTwo();
    }

    //此方法只是关闭软键盘
    public void hintKbTwo() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive() && getCurrentFocus() != null) {
            if (getCurrentFocus().getWindowToken() != null) {
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    protected void setNavigationView() {
        navigationView.getTitle().setBackgroundColor(getResources().getColor(R.color.white));
        navigationView.getNavigationTitleTxt().setTextColor(getResources().getColor(R.color.bleak));
        navigationView.getGoBackBtn().setVisibility(View.GONE);
        navigationView.getGoBackBtn2().setVisibility(View.VISIBLE);
        navigationView.getGoBackBtn2().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    protected void setTitleTxt(String title) {
        navigationView.getNavigationTitleTxt().setText(title);
    }

    protected void setOperationBtn(String title) {
        navigationView.getOperationBtn().setText(title);
    }

    protected TextView getViewOperationBtn() {
        return navigationView.getOperationBtn();
    }



    protected void onViewCreated() {

    }

    @Override
    public void getGoBackBtn() {
        finish();
    }

    @Override
    public void getOperationBtn() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        hintKbTwo();
    }

    public void cityName() {

    }

    protected abstract int getLayout();

    protected abstract void initData();

}
